<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * BackendPro
 *
 * An open source development control panel written in PHP
 *
 * @package		BackendPro
 * @author		Adam Price
 * @copyright	Copyright (c) 2008, Adam Price
 * @license		http://www.gnu.org/licenses/lgpl.html
 * @link		http://www.kaydoo.co.uk/projects/backendpro
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Welcome
 *
 * The default welcome controller
 *
 * @package  	BackendPro
 * @subpackage  Controllers
 */
class Welcome extends Public_Controller
{
	function Welcome( ){
		parent::Public_Controller( );
		
		$this->load->library( "template" );
		//needed for weather api
		$this->load->library( "xmltoarray" );
		//needed for forms
		$this->load->helper( "form" );
		//needed for checkin24 support
		$this->load->library("Nusoap");
		//needed for the guestbook captcha
		$this->load->library("Recaptcha_lib");
		
		// Load the form module
		$this->lang->module_load( 'form', 'form' );
		
		$this->load->module_model( "article", "menu_model" );
		$this->load->module_model( "article", "article_model" );
		$this->load->module_model( "article", "corporate_model" );
		$this->load->module_model( 'article', 'website_model' );
		$this->load->module_model( "blog", "blog_model" );
		$this->load->module_model( "form", "form_model" );
		$this->load->module_model( "checkin24", "checkin24_model" );
		$this->load->module_model( "events", "events_model" );
		$this->load->module_model( "guestbook", "guestbook_model" );
		
		// Load the article language module
		$this->lang->module_load( 'welcome', 'welcome' );
		$this->lang->module_load( 'weather', 'weather' );
	}

	function index( ){
		// Display Page
		if( is_user( ) ){
			//echo "Angemeldet";
			// Include dashboard Javascript code
			$this->bep_assets->load_asset('bep_dashboard');
			// Load the dashboard library
			$this->load->module_library('dashboard','dashboard');
			// Load any widget libraries
			$this->load->module_library('dashboard','Statistic_widget');
			// Assign widgets to dashboard
			//Debug Mode for administrator
			if( $this->session->userdata('group_id') == 2 ){
				$this->dashboard->assign_widget( new widget( $this->lang->line( 'debug_info' ), $this->session->userdata ) );
			} //if
			$this->dashboard->assign_widget( new widget( $this->lang->line('add_article'), 
														'Hier konnen Sie einen neuen Artikel hinzuf&uuml;gen.<br />' ), 
														'left');
			if( $this->session->userdata('template') == 'trassenheide' ){
				$idSite = 6;
				$start = date( "Y-m-d", time() - ( 31 * 24 * 60 * 60 ) );
				$end = date( "Y-m-d" );
				$this->dashboard->assign_widget( new widget( $this->lang->line('statistics'),
												'<object style="visibility: visible;" id="VisitsSummarygetEvolutionGraphChart_swf" data="http://www.conlabz.de/report/libs/open-flash-chart/open-flash-chart.swf?piwik=0.4.3" bgcolor="#FFFFFF" type="application/x-shockwave-flash" height="150" width="100%">
													<param value="always" name="allowScriptAccess">
													<param value="opaque" name="wmode">
													<param value="data-file=http%3A%2F%2Fwww.conlabz.de%2Freport%2Findex.php%3Fmodule%3DVisitsSummary%26action%3DgetEvolutionGraph%26columns%5B%5D%3Dnb_visits%26idSite%3D<?php echo $idSite; ?>%26period%3Dday%26date%3D<?php echo $start; ?>%2C<?php echo $end; ?>%26viewDataTable%3DgenerateDataChartEvolution&loading=Laden..." name="flashvars">
												</object>' ),
												'right');
			}
			// Load dashboard onto page
			$data['dashboard'] = $this->dashboard->output();
			
			$data['header'] = "User Control Panel";
			$data['page'] = $this->config->item( 'backendpro_template_user' ) . '/welcome';
			if( isset ( $_GET['page'] ) ){
				$data['page'] = $_GET['page'];
			}
			$data['menu'] = $this->website_model->getWebsiteUserMenu( );
			$data['module'] = 'article';
            $this->load->module_view( $data['module'], 'user/container', $data );
			//echo "FEHLER";
			//$this->template->write_module_view( 'article', 'navigation', 'user/menu', $data );
			//$this->template->write_module_view( 'article', 'content', 'user/welcome' );
		    // Render the template
		    //$this->template->set_template( 'admin' );
		    //$this->template->render();
		} else {
			$host = $_SERVER['HTTP_HOST'];
			$this->load->module_model( 'article', 'website_model' );
			$website = $this->website_model->getWebsiteByDomain( $host );
			$this->session->set_userdata( array( "website_id" => $website['id'] ) );
			
			$this->_set_regions( $host, NULL, NULL, NULL, true, $this->_set_lang( ) );
			
			$this->template->render( );
		}
	}
	
	function _set_lang( ){
		//should be taken form database
		switch( $this->lang->lang( ) ){
			case 'en':
				$lang = 2;
				break;
			default:
				$lang = 1;
				break;
		}
		return $lang;
	}
	
	function page( ){
		// Display Page
		$host = $_SERVER['HTTP_HOST'];
		$menu_url = $this->uri->segment( 4 );
		//get type and id by menu url
		$website = $this->website_model->getWebsiteByDomain( $host );
		$type_array = $this->menu_model->getTypeArrayByUrl( $menu_url, $website['id'] );
		
		$this->session->set_userdata( array( "website_id" => $website['id'] ) );
		
		$this->_set_regions( $host, $type_array['type'], $type_array['type_id'], $menu_url, true, $this->_set_lang( ) );
		
		$this->template->render( );
	}
	
	function blog( $blog_id, $entry_id ){
		// Display Blog Entry Page
		$host = $_SERVER['HTTP_HOST'];
		//get type and id by menu url
		$website = $this->website_model->getWebsiteByDomain( $host );
		
		$this->session->set_userdata( array( "website_id" => $website['id'] ) );
		
		$entry = $this->blog_model->getEntriesView( $blog_id, $entry_id );
		$data['h1'] = $entry['entry'][$entry_id]['title'];
		$data['text'] = array( '0' => array( 	'title' => '',
								'timestamp' => $entry['entry'][$entry_id]['timestamp'],
								'body' => $entry['entry'][$entry_id]['body']
							) ); 
		$this->template->write_view( 'body', 'template/'.$website['template'].'/blog', $data );
		$this->_set_regions( $host, '', '', '', false, $this->_set_lang( ) );
		
		$this->template->render( );
	}

	function search( ){
		$host = $_SERVER['HTTP_HOST'];
		$website = $this->website_model->getWebsiteByDomain( $host );
		$this->session->set_userdata( array( "website_id" => $website['id'] ) );
		$data = array( );
		$data['search_string'] = $this->input->post( 'search' );
		$data['result'] = $this->article_model->search( $data['search_string'] );
		$this->template->write_view( 'body', 'public/results', $data );
		$this->_set_regions( $host, '', '', '', false, $this->_set_lang( ) );
		
		$this->template->render( );
	}
	
	/*
	 * bring content to the regions
	 */
	function _set_regions( $host, $type = NULL, $type_id = NULL, $menu_url = NULL, $set_body = true, $lang = 1 ){
		$sql_content = "SELECT `website`.`template`, `region_content`.`type`, `region_content`.`value`, `region`.`region` FROM `region_content`, `region`, `website`, `domain`
      								   WHERE  `region_content`.`region_id` = `region`.`id` 
      								   AND `region`.`website_id` = `website`.`id` 
      								   AND `website`.`id` = `domain`.`website_id` 
      								   AND `domain`.`url` = '" . $host . "'";
		$content = $this->db->query( $sql_content );
		$data = array( );
		$data['parent'] = $this->menu_model->getParent( $menu_url );
		//print( "PARENT:" );
		//print_r( $data['parent'] );
		foreach( $content->result( ) as $row ){
			$template = $row->template;
			$data['menu_url'] = $menu_url;
			//print "TYPE:".$row->type;
			if( file_exists( PREPATH.DEVPATH.PATH.'views/template/'.$template.'/'.$row->type.EXT ) ){
				//array needs to be filled with menu title and returned as array
				//@todo: needs to be easier!!!
				if( $row->type == "top" || $row->type == "header" || $row->type == "navigation" ){
					$data[$row->type] = $this->menu_model->getMenuList( $row->value, $lang );
				}
				//print_r( $data[$row->type] );
				//$this->template->write_module_view( 'welcome', $row->region, 'template/'.$template.'/'.$row->type, $data[$row->type] );
				$this->template->write_view( $row->region, 'template/'.$template.'/'.$row->type, $data );
				unset( $data );
			}
			
		}

		if( $set_body ){
			//get content area
			//first id is empty 
			if( empty( $type ) ){
				//echo "EMPTY TYPE";
				//call front page, so take a look at the menu and find the default page for the website
				$default = $this->db->query( "SELECT `menu`.`type`, `menu`.`type_id`, `menu`.`template` FROM `menu`, `menu_position`, `website`, `domain`
	      								   WHERE `menu`.`default` = '1'  
	      								   AND `menu`.`menu_position_id` = `menu_position`.`id` 
	      								   AND `menu_position`.`website_id` = `website`.`id` 
	      								   AND `website`.`id` = `domain`.`website_id` 
	      								   AND `domain`.`url` = '" . $host . "'" );
				$rowFrontPage = $default->result_array( );
				//print_r( $rowFrontPage );
				if( !empty( $rowFrontPage ) ) {
					$type_id = $rowFrontPage[0]['type_id'];
				}
			} //if
			//echo "<pre>Hier";
			//print_r( $type );
			//echo "</pre>";
			switch( $type ){
				//@todo: clean up
				case 'imprint':
					$row = $this->corporate_model->getCorporate( );
					$data['h1'] = "Impressum";
					$data['text'] = $row;
					break;
				case 'blog':
					$row = $this->blog_model->getEntriesView( $type_id );
					if( !empty( $row ) ){
						$data['h1'] = $row['h1'];
						if( !empty( $row['entry']) ) 
							$data['text'] = $row['entry'];
					}
					break;
				case 'guestbook':
					$this->lang->module_load( 'guestbook', 'guestbook' );
					$row = $this->guestbook_model->getEntriesView( $type_id );
					if( !empty( $row ) ){
						$data['h1'] = $row['h1'];
						if( !empty( $row['entry']) ) 
							$data['text'] = $row['entry'];
						$data['publickey'] = $row['recaptcha_public_key'];
						$data['guestbook_id'] = $type_id;
					}
					break;
				case 'form':
					$row = $this->form_model->getFormView( $type_id );
					if( !empty( $row ) ){
						$data['h1'] = $row['h1'];
						$data['text'] = $row['fields'];
						$data['description'] = $row['description'];
						$data['form_id'] = $type_id;
					}
					break;
				case 'checkin24':
					$row = $this->checkin24_model->getObjectView( $type_id ); //, 'price', SORT_DESC );
					if( !empty( $row ) ){
						$data['h1'] = $row['h1'];
						$data['text'] = $row['object'];
					}
					break;
				case 'events':
					$row = $this->events_model->getEventView( $type_id );
					$data['h1'] = $this->menu_model->getMenuTitleByUrl( $menu_url );
					$data['text'] = $row;
					break;
				case 'article':
				default:
					$body = $this->article_model->getArticles( "`article`.`id` = '".$type_id."' AND article.category_language_id='".$lang."'" );
					$row = $body->result_array( );
					if( empty( $row ) ){
						//selected language is empty, so take the default language
						$body = $this->article_model->getArticles( "`article`.`id` = '".$type_id."' AND article.category_language_id='1'" );
						$row = $body->result_array( );
						$data['h1'] = $row[0]['article_title'];
						$data['text'] = $row[0]['body_text'];
					} else {
						$data['h1'] = $row[0]['article_title'];
						$data['text'] = $row[0]['body_text'];
					}
					break;
			}
			
			if( !empty( $data ) ){
				$body = '<h1>' . $data['h1'] . '</h1>';
				if( !empty( $data['text'] ) ) 
					$body .= $data['text'];
				//@todo: needs to write in a special view per type...
				//if file exists in the template path of the project take this
				//else take default in the module
				//print "WICHTIG:".PREPATH.DEVPATH.PATH.'views/template/'.$template.'/'.$type.EXT;
				if( file_exists( PREPATH.DEVPATH.PATH.'views/template/'.$template.'/'.$type.EXT ) ){
					//echo "FEHLER2";
					$this->template->write_view( 'body', 'template/'.$template.'/'.$type, $data );
				}  elseif( file_exists( PREPATH.DEVPATH.MODPATH.'blog/views/view_'.$type.EXT ) ){
					//@todo: doesn't work, but needs to, can't load view from another module
					//echo "FEHLER3";
					$this->template->write_view( 'body', MODPATH.'blog/views/view_'.$type, $data );
				} else {
					//echo "FEHLER4";
					$this->template->write( 'body', $body, TRUE );
				}
			} 
		}
	}
	
}


/* End of file welcome.php */
/* Location: ./modules/welcome/controllers/welcome.php */
